package com.sheyu.mall.seckill.controller;

import com.sheyu.common.utils.R;
import com.sheyu.mall.seckill.service.SeckillService;
import com.sheyu.mall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sheyu
 * @Desciption:
 * @date 2020/9/24 5:14 下午
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @GetMapping(value = "/getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> seckillSkuRedisToList = seckillService.getCurrentSeckillSkus();

        return R.ok().setData(seckillSkuRedisToList);
    }

    /**
     * 根据skuId查询商品是否参加秒杀活动
     * @param skuId
     * @return
     */
    @GetMapping(value = "/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo skuRedisTo = seckillService.getSkuSeckilInfo(skuId);
        return R.ok().setData(skuRedisTo);
    }

    @GetMapping(value = "/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){
        String orderSn = null;
        try{
            //1、判断是否登陆
            orderSn = seckillService.kill(killId,key,num);
            model.addAttribute("orderSn",orderSn);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "success";
    }

}
